import { IHook } from '../types'
type requestType = 'pending' | 'done'

/**请求状态管理 */
const requestState = new Map<string, requestType>()

/**请求防抖 */
export const debounceHook: IHook = {
  request({ url, ctr }) {
    if (!ctr.debounce) return

    const key = url
    if (requestState.get(key) === 'pending') {
      console.log(`${key} 防抖中...`)
      return Promise.reject({ breakName: 'debounceHook', data: url })
    }
    requestState.set(key, 'pending')
  },
  response({ url, ctr }) {
    if (!ctr.debounce) return

    const key = url
    requestState.set(key, 'done')
  }
}
